﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using IFRS.Web;
using IFRS.DataAccess.DAL;
using System.Data;
using IFRS.Models;
using IFRS_Global;
using System.Globalization;

public partial class IFRS_WIN15_2E : PageBase
{
    #region "---------- Property -----------"

    private CollateralManual CollateralManual = new CollateralManual();
    private COLLATERALMANUAL COLLATERALMANUAL = new COLLATERALMANUAL();

    #endregion

    #region "---------- Events -----------"

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!(Page.IsPostBack || Page.IsCallback))
        {
            this.ViewMenu();
            this.BindDLL();
            this.AddClientScript();
            SetErrorMessage();
            if (Request.QueryString["id"].ToString()!=null)
            this.BindData(Convert.ToInt32(Request.QueryString["id"].ToString()));
        }
        else
            ClientScript.RegisterHiddenField("isPostBack", "1");
    }

    #endregion

    protected void btnSAVE_Click(object sender, EventArgs e)
    {
        SaveData();
    }
    protected void btnCANCEL_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/IFRS/WIN15_1S.aspx");
    }

    #region "----- Methods -------"

    private void ViewMenu()
    {
        this.MenuTitleBar = Resources.ifrsMenu.WIN15_2E;
    }
    private void BindDLL()
    {
        ddlSourceData.SetData(DDL_TYPE.SourceData, "", false, true);
    }

    private void AddClientScript()
    {
        btnColleteralType.Attributes.Add("onClick", "return popWindow('" + ResolveUrl("~/PopupPages/CollateralType.aspx") + "','700','550');");

        txtColleteralType.AddControl(hidColleteralType.ClientID);
        txtColleteralType.AddControl(txtColleteralType.ClientID);
        txtColleteralType.ControlSearch = txtColleteralType.ClientID;
        txtColleteralType.AddQueryString("bIsActive", "1");
        txtColleteralType.SearchKey = "CollateralType_oCollateralTypeCode_oCollateralTypeNameTH";
    }

    private void SaveData()
    {
        CollateralManual.CollateralManualID = hidCollateralManualID.Value.AsInt32();
        CollateralManual.TransactionDate = txtDATE_TRANSACTION.Text.StringToDateAllowNull();
        CollateralManual.AccountNo = txtAccountNo.Text;
        CollateralManual.CollateralValue = txtAmount.Decimal.Value;
        CollateralManual.SourceDataID = ddlSourceData.SelectedValue.AsInt32();
        CollateralManual.CollateralTypeID = hidColleteralType.Value.AsInt32();
        CollateralManual.CollateralNo = txtCollateralNumber.Text;
        CollateralManual.IsConfirmed = true;
        CollateralManual.SetStandardField();

        int iID = COLLATERALMANUAL.UPDATE(CollateralManual);
        if (iID > 0)
        {
            this.JS_Alert(Resources.ifrsResource.SaveData);
            BindData(hidCollateralManualID.Value.AsInt32());
        }
        else
        {
            Utility.AlertMessage(Page, Resources.ifrsResource.CanNotSaveData);
        }
    }

    private void SetErrorMessage()
    {
        btnSAVE.ValidationGroup = "Master";
        reqAmount.SetRequiredField(lblAmount, txtAmount, btnSAVE);
        reqddlSourceData.SetRequiredField(lblSourceData, ddlSourceData, btnSAVE);
        reqDATE_TRANSACTION.SetRequiredField(lblDATE_TRANSACTION, txtDATE_TRANSACTION, btnSAVE);
        reqAccountNo.SetRequiredField(lblAccountNo, txtAccountNo, btnSAVE);
        reqCollateralNumber.SetRequiredField(lblCollateralNumber, txtCollateralNumber, btnSAVE);
        reqColleteralType.SetRequiredField(lblColleteralType, txtColleteralType, btnSAVE);
    }

    private void BindData(int ID)
    {
        var COLLATERALMANUAL = new COLLATERALMANUAL();
        DataTable dtCOLLATERALMANUAL = COLLATERALMANUAL.SEARCH_BYID(ID);
        List<CollateralManual> CollateralManual = new List<CollateralManual>();
        ConvertHelper.ConvertDataTableToObject(dtCOLLATERALMANUAL, CollateralManual);
        if (dtCOLLATERALMANUAL.Rows.Count > 0)
        {
            hidCollateralManualID.Value = CollateralManual[0].CollateralManualID.ToString();

            //txtDATE_TRANSACTION.Text = CollateralManual[0].TransactionDate.Value.ToString("dd/MM/yyyy");
            txtDATE_TRANSACTION.Text = CollateralManual[0].TransactionDate.DateToString();

            if (CollateralManual[0].SourceDataID.ToString() != "")
                ddlSourceData.SelectedValue = CollateralManual[0].SourceDataID.ToString();

            txtAccountNo.Text = CollateralManual[0].AccountNo;

            if (CollateralManual[0].CollateralTypeID.ToString() != "")
            {
                hidColleteralType.Value = CollateralManual[0].CollateralTypeID.ToString();
                txtColleteralType.Text = CollateralManual[0].CollateralTypeNameTH.ToString();
            }

            txtAmount.Text = CollateralManual[0].CollateralValue.ToString();
            txtCollateralNumber.Text = CollateralManual[0].CollateralNo;
        }
    }

    #endregion

}